package com.gitee.l0km.xthrift.thrift.codec;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.concurrent.Immutable;

import org.apache.thrift.protocol.TProtocol;

import com.facebook.swift.codec.ThriftCodec;
import com.facebook.swift.codec.ThriftCodecManager;
import com.facebook.swift.codec.ThriftProtocolType;
import com.facebook.swift.codec.internal.TProtocolReader;
import com.facebook.swift.codec.internal.reflection.ReflectionThriftStructCodec;
import com.facebook.swift.codec.metadata.ThriftConstructorInjection;
import com.facebook.swift.codec.metadata.ThriftFieldInjection;
import com.facebook.swift.codec.metadata.ThriftFieldMetadata;
import com.facebook.swift.codec.metadata.ThriftInjection;
import com.facebook.swift.codec.metadata.ThriftMethodInjection;
import com.facebook.swift.codec.metadata.ThriftParameterInjection;
import com.facebook.swift.codec.metadata.ThriftStructMetadata;
import com.gitee.l0km.com4j.basex.annotations.ActiveOnClass;
import com.gitee.l0km.xthrift.base.BaseThriftUtils;
import com.gitee.l0km.xthrift.thrift.protocol.TProtocolReaderXthrift;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;

import gu.sql2java.BaseBean;

import static com.facebook.swift.codec.metadata.FieldKind.THRIFT_FIELD;
import static com.gitee.l0km.xthrift.thrift.protocol.TProtocolSupport.getFieldName;
import static java.lang.String.format;

/**
 * <a href="https://gitee.com/l0km/sql2java">sql2java 4.x</a> gu.sql2java.BaseBean 接口实例的编解码实现<br>
 * 重写{@link ReflectionThriftStructCodec#read(TProtocol)}方法,
 * BaseBean 在所有其他字段被注入后才执行实例的内置字段{@code initialized},{@code modified},
 * 以实现不论{@link ThriftStructMetadata#getMethodInjections()}返回的字段顺序如何,内置字段都在最后注入
 * @author guyadong
 *
 * @param <T>
 */
@Immutable
@ActiveOnClass(BaseBean.class)
public class BaseBeanReflectionCodec<T> extends FilterableThriftStructCodec<T>
{
	private final ThriftCodec<Integer> I32_CODEC;
	private final ThriftCodec<Long> I64_CODEC;
	private final ThriftCodec<Short> I16_CODEC;
	private final ThriftCodec<Byte> BYTE_CODEC;

	public BaseBeanReflectionCodec(ThriftCodecManager manager, ThriftStructMetadata metadata)
	{
		super(manager, metadata);
		I32_CODEC =  manager.getCodec(int.class);
		I64_CODEC =  manager.getCodec(long.class);
		I16_CODEC =  manager.getCodec(short.class);
		BYTE_CODEC =  manager.getCodec(byte.class);
	}
	
	private String fieldName(TProtocolReader reader) {
		if (reader.getFieldType() == -1) {
			return getFieldName(reader);
		}else {
			return metadata.getField(reader.getFieldId()).getName();
		}
	}
	
	@Override
	protected Object readField(TProtocolReader reader, ThriftCodec<?> codec) throws Exception {
		if(reader.getFieldType() != codec.getType().getProtocolType().getType() 
				&& BaseThriftUtils.BASEBEAN_BUILTIN_FIELDS.contains(fieldName(reader))){
			/** 保持向下兼容，当 initialized,modified字段为整型时按整型解析并转为HEX字符串返回 */
			if(ThriftProtocolType.STRING.equals(codec.getType().getProtocolType())) {
				if (reader instanceof TProtocolReaderXthrift) {
					return ((TProtocolReaderXthrift)reader).readStringOrNumberAsHex();
				}else if(ThriftProtocolType.I32.getType() == reader.getFieldType()) {
					return toHex((Number)reader.readField(I32_CODEC));
				}else if(ThriftProtocolType.I64.getType() == reader.getFieldType()) {
					return toHex((Number)reader.readField(I64_CODEC));
				}else if(ThriftProtocolType.I16.getType() == reader.getFieldType()) {
					return toHex((Number)reader.readField(I16_CODEC));
				}else if(ThriftProtocolType.BYTE.getType() == reader.getFieldType()) {
					return toHex((Number)reader.readField(BYTE_CODEC));
				}
			}
		}
		return super.readField(reader, codec);
	}
	@Override
	protected T afterConstruct(T instance, Set<String> modifiedReset, Set<String> modifiedSet) {
		BaseBean bean = (BaseBean)instance;
		if(!modifiedReset.isEmpty()) {
			bean.resetModified(modifiedReset.toArray(new String[modifiedReset.size()]));
		}
		if(!modifiedSet.isEmpty()) {
			bean.modified(modifiedSet.toArray(new String[modifiedSet.size()]));
		}
		return instance;
	}
	@SuppressWarnings("unchecked")
    protected T constructStruct(Map<Short, Object> data)
            throws Exception
    {
        // construct instance
        Object instance;
        {
            ThriftConstructorInjection constructor = metadata.getConstructorInjection().get();
            Object[] parametersValues = new Object[constructor.getParameters().size()];
            for (ThriftParameterInjection parameter : constructor.getParameters()) {
                Object value = data.get(parameter.getId());
                parametersValues[parameter.getParameterIndex()] = value;
            }

            try {
                instance = constructor.getConstructor().newInstance(parametersValues);
            }
            catch (InvocationTargetException e) {
                if (e.getTargetException() != null) {
                    Throwables.throwIfInstanceOf(e.getTargetException(), Exception.class);
                }
                throw e;
            }
        }

        // inject fields
        for (ThriftFieldMetadata fieldMetadata : metadata.getFields(THRIFT_FIELD)) {
            for (ThriftInjection injection : fieldMetadata.getInjections()) {
                if (injection instanceof ThriftFieldInjection) {
                    ThriftFieldInjection fieldInjection = (ThriftFieldInjection) injection;
                    Object value = data.get(fieldInjection.getId());
                    if (value != null) {
                        fieldInjection.getField().set(instance, value);
                    }
                }
            }
        }
        List<ThriftMethodInjection> methodInjections = Lists.newArrayList(metadata.getMethodInjections());
        final List<ThriftMethodInjection> builtinInjections = Lists.newArrayList();
        // 将BaseBean内置字段排除保存到 builtinInjections
        for(Iterator<ThriftMethodInjection> itor = methodInjections.iterator();itor.hasNext();){
        	ThriftMethodInjection input = itor.next();
        	if(BaseThriftUtils.BASEBEAN_BUILTIN_FIELDS.contains(input.getParameters().get(0).getName())){
        		builtinInjections.add(input);
        		itor.remove();
        	}
        }
        
        // inject methods
        injectMethods(data,instance,methodInjections);
        // 所有其他字段都执行完成注入后再执行内置字段的注入
        injectMethods(data,instance,builtinInjections);

        // builder method
        if (metadata.getBuilderMethod().isPresent()) {
            ThriftMethodInjection builderMethod = metadata.getBuilderMethod().get();
            Object[] parametersValues = new Object[builderMethod.getParameters().size()];
            for (ThriftParameterInjection parameter : builderMethod.getParameters()) {
                Object value = data.get(parameter.getId());
                parametersValues[parameter.getParameterIndex()] = value;
            }

            try {
                instance = builderMethod.getMethod().invoke(instance, parametersValues);
                if (instance == null) {
                    throw new IllegalArgumentException("Builder method returned a null instance");

                }
                if (!metadata.getStructClass().isInstance(instance)) {
                    throw new IllegalArgumentException(format("Builder method returned instance of type %s, but an instance of %s is required",
                            instance.getClass().getName(),
                            metadata.getStructClass().getName()));
                }
            }
            catch (InvocationTargetException e) {
                if (e.getTargetException() != null) {
                    Throwables.throwIfInstanceOf(e.getTargetException(), Exception.class);
                }
                throw e;
            }
        }

        return (T) instance;
    }
    
    private static void injectMethods(Map<Short, Object> data,Object instance,Iterable<ThriftMethodInjection> methodInjections) throws Exception{
        for (ThriftMethodInjection methodInjection : methodInjections) {
            boolean shouldInvoke = false;
            Object[] parametersValues = new Object[methodInjection.getParameters().size()];
            for (ThriftParameterInjection parameter : methodInjection.getParameters()) {
                Object value = data.get(parameter.getId());
                if (value != null) {
                    parametersValues[parameter.getParameterIndex()] = value;
                    shouldInvoke = true;
                }
            }

            if (shouldInvoke) {
                try {
                    methodInjection.getMethod().invoke(instance, parametersValues);
                }
                catch (InvocationTargetException e) {
                    if (e.getTargetException() != null) {
                        Throwables.throwIfInstanceOf(e.getTargetException(), Exception.class);
                    }
                    throw e;
                }
            }
        }
    }
	/**
	 * cast integral Number  to HEX string
	 * @param input
	 * @return hex string or  null if not Number
	 */
	private static String toHex(Number input){
		if(input instanceof Integer) {
			return String.format("%08X",input.intValue());
		}else if(input instanceof Long) {
			return String.format("%016X",input.longValue());
		}else if(input instanceof Short) {
			return String.format("%04X",input.shortValue());
		}else if(input instanceof Byte) {
			return String.format("%02X",input.byteValue());
		}else if(null != input) {
			return String.format("%08X",input.intValue());
		}
		return null;
	}
}
